package com.cn.kabalifeuser.ui.integral;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.integral.activity.CartListActivity;
import com.cn.kabalifeuser.ui.integral.activity.CommoditySearchActivity;
import com.cn.kabalifeuser.ui.integral.activity.TypeAllActivity;
import com.cn.kabalifeuser.ui.integral.fragment.SelectedFragments;
import com.cn.kabalifeuser.ui.integral.fragment.ShoppingMallFragments;
import com.cn.kabalifeuser.ui.my.adapter.PagerAdapter;
import com.cn.kabalifeuser.ui.service.bean.IndexTypeListBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.flyco.roundview.RoundTextView;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/16.
 * function: activiyt 的积分商城
 */
public class IntegralMainActivity extends IBaseActivity<FragmentIntegralView, FragmentIntegralPresenter> implements FragmentIntegralView, OnTabSelectListener {

    @BindView(R.id.slitab)
    SlidingTabLayout slitab;
    @BindView(R.id.vp_type)
    ViewPager vpType;
    List<String> mList = new ArrayList<>();
    PagerAdapter mAdapter;
    @BindView(R.id.tv_msg)
    RoundTextView tvMsg;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected FragmentIntegralPresenter createPresenter() {
        return new FragmentIntegralPresenter();
    }


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_integral;
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onTypeSuccess(IndexTypeListBean bean) {
        for (int i = 0; i < bean.getResult().size(); i++) {
            mList.add(bean.getResult().get(i).getTitle());
            if (i == 0)
                mFragments.add(SelectedFragments.getInstance(i));
            else
                mFragments.add(ShoppingMallFragments.getInstance(bean.getResult().get(i).getId()));
        }
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mList, mFragments);
        vpType.setAdapter(mAdapter);
        vpType.setOffscreenPageLimit((mList.size() - 1));
        slitab.setViewPager(vpType);
        slitab.setOnTabSelectListener(this);
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        //分类
        mPresenter.onTypeData(LoginHelper.getLonginData().getResult(), 3, 3);
    }

    @OnClick({R.id.top_left_view, R.id.ll_btn_right, R.id.tv_btn_all, R.id.img_btn_cart, R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_left_view:
                finish();
                break;
            case R.id.ll_btn_right:
                break;
            case R.id.tv_btn_all:
                //全部
                $startActivity(TypeAllActivity.class);
                break;
            case R.id.img_btn_cart:
                //todo  购物车
                $startActivity(CartListActivity.class);
                break;
            case R.id.ll_search:
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 1);
                $startActivity(CommoditySearchActivity.class, bundle);
                break;
        }
    }

}
